<?php
class Login extends TPage
{
	public function onInit($param){
		parent::onInit($param);
		Propel::init($this->Application->Parameters["PropelConfigFile"]);
	}

	public function onLoad($param){
		parent::onLoad($param);
		//$this->ClientScript->registerEndScript("HideMenu", "Hide('menucontainer');");
		$this->Master->menucontainer->Visible = false;
		$this->Master->lblBienvenido->Visible = false;
		$this->Master->lblDateTitle->Visible = false;
		$this->Master->lblDate->Visible = false;
		$this->Master->hlHelp->Visible = false;
		$this->Master->hlPassword->Visible = false;
		$this->Master->hlClose->Visible = false;
		$this->Username->Attributes->add("autocomplete", "off");

	}

    /**
     * Validates whether the username and password are correct.
     * This method responds to the TCustomValidator's OnServerValidate event.
     * @param mixed event sender
     * @param mixed event parameter
     */
    public function cvValidateUser_OnServerValidate($sender, $param){
		$auth =	new  UserAuthentication();
		if(!$auth->ValidateUser($this->Username->Text, $this->Password->Text)){
			$param->IsValid=false;
		}
    }

    /**
     * Redirects the user's browser to appropriate URL if login succeeds.
     * This method responds to the login button's OnClick event.
     * @param mixed event sender
     * @param mixed event parameter
     */
    public function buttonLogin_OnClick($sender,$param){
       if($this->Page->IsValid){
       		$id= $_SESSION["IdUsuario"];
	       	$oUsuario= UsuarioPeer::retrieveByPK($id);
       		if(is_object($oUsuario)){
       			$Logon = new THttpCookie("Logon", $this->Username->Text);
       			$this->Response->Cookies->add($Logon);
       			$_SESSION["Logon"] = $this->Username->Text;
				$Rol= $oUsuario->getIdrol();
       			if($Rol==4){//Rol de Asociado
					 //$idAsociado=AsociadosPeer::GetIdAsociado($id);
       				 $idAsociado =$oUsuario->getAsociado();
					 $this->Response->redirect("?page=Cuotas.CuotasAsociados&id=".$idAsociado);
       			}else
       				{
       			 	 $this->Response->redirect("?page=Home");
       				}
        	}
       }
    }
}

?>